package com.zxl.readsms

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import java.util.regex.Pattern


class AddMailAct: AppCompatActivity() {
    private lateinit var rcv :RecyclerView
    private lateinit var mAdapter : FruitAdapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_mail)
        rcv = findViewById(R.id.rcv)
        val layoutManager =  LinearLayoutManager(this);
        rcv.layoutManager = layoutManager;
        mAdapter =  FruitAdapter(MAIL_CC_ADDRESS)
        rcv.adapter = mAdapter
    }

    fun addEvent(view: View) {
        val et=findViewById<EditText>(R.id.et)
        val str=et.text.toString()
        if (isMail(str)){
            if (MAIL_CC_ADDRESS.contains(str)){
                Toast.makeText(this, "已包含该邮箱", Toast.LENGTH_SHORT).show()
                return
            }
            MAIL_CC_ADDRESS.add(0,str)
            mAdapter.notifyItemInserted(0)
            et.setText("")
        }else{
            Toast.makeText(this, "请输入有效的邮箱地址", Toast.LENGTH_SHORT).show()
            et.setText("")
        }
    }
    private fun isMail(url: String): Boolean {
        if (url.isNullOrEmpty()) return false
        val pattern = "[\\w!#\$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#\$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?"
        val r = Pattern.compile(pattern)
        val m = r.matcher(url)
        return m.matches()
    }
    inner class FruitAdapter(mFruitList: List<String>) :
        RecyclerView.Adapter<FruitAdapter.ViewHolder>() {
        private val mFruitList: List<String>

        inner class ViewHolder(@NonNull view: View) : RecyclerView.ViewHolder(view) {
            var fruitName: TextView
            init {
                fruitName = view.findViewById<View>(android.R.id.text1) as TextView
            }
        }

        @NonNull
        override fun onCreateViewHolder(
            @NonNull parent: ViewGroup,
            viewType: Int
        ): ViewHolder {
            val view: View =
                LayoutInflater.from(parent.context).inflate(android.R.layout.simple_list_item_1, parent, false)
            return ViewHolder(view)
        }

        override fun onBindViewHolder(@NonNull holder: ViewHolder, position: Int) {
            val fruit: String = mFruitList[position]
            holder.fruitName.text = fruit
            holder.fruitName.setOnLongClickListener {
                MAIL_CC_ADDRESS.remove(fruit)
                mAdapter.notifyDataSetChanged()
                true
            }
        }

        override fun getItemCount(): Int {
            return mFruitList.size
        }

        init {
            this.mFruitList = mFruitList
        }
    }

}